package com.ys.controller.web.business;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.entity.business.YsbExportLog;
import com.ys.enume.business.YsbExportLogEnum;
import com.ys.util.shiro.ShiroUtils;
import com.ys.util.DateUtil;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/business/tpYsbExportLog")
public class WebYsbExportLogController {
	
	/*
	 * 导出
	 */
	@GetMapping("/toExport")
	public String toExport(Model model, HttpServletRequest request, HttpServletResponse response, Long menuId){
		
		String loginUserId = ShiroUtils.getUserId();
		if (loginUserId == null) {
			return W.error(model, "尚未登录，请先登录");
		}
		
		if (menuId == null) {
			return W.error(model, "菜单ID不能为空");
		}
		
		List<YsbExportLog> listYsbExportLog = new YsbExportLog()
				.setCreateUser(loginUserId)
				.setMenuId(menuId)
				.setOrderby("A.Id desc")
				.queryList();
		
		List<ToExportDto.RowDto> rows = new ArrayList<ToExportDto.RowDto>();
		for(YsbExportLog ysbExportLog : listYsbExportLog) {
			Long id = ysbExportLog.getId();
			String stateName = YsbExportLogEnum.State.getName(ysbExportLog.getState());
			String createTime = DateUtil.getYYYY_MM_DD_HH_mm_ss(ysbExportLog.getCreateTime());
			String updateTime = ysbExportLog.getState() == YsbExportLogEnum.State.SUCCESS.value ? DateUtil.getYYYY_MM_DD_HH_mm_ss(ysbExportLog.getUpdateTime()) : "-";
			String url = ysbExportLog.getUrl();
			rows.add(new ToExportDto.RowDto(id, stateName, createTime, updateTime, url,null));
		}
		
		ToExportDto dto = new ToExportDto(rows);
		model.addAttribute("dto", dto);
		return "business/tpYsbExportLog/toExport";
	}

	/*
	 * 导出线上采购入库单独使用
	 */
	@GetMapping("/toExportBid")
	public String toExportBid(Model model, HttpServletRequest request, HttpServletResponse response, Long menuId){

		String loginUserId = ShiroUtils.getUserId();
		if (loginUserId == null) {
			return W.error(model, "尚未登录，请先登录");
		}

		if (menuId == null) {
			return W.error(model, "菜单ID不能为空");
		}

		List<YsbExportLog> listYsbExportLog = new YsbExportLog()
				.setMenuId(menuId)
				.setOrderby("A.Id desc")
				.where("A.CreateTime > DATEADD(day, -7, CAST(GETDATE() AS date))")
				.queryList();

		List<ToExportDto.RowDto> rows = new ArrayList<ToExportDto.RowDto>();
		for(YsbExportLog ysbExportLog : listYsbExportLog) {
			Long id = ysbExportLog.getId();
			String stateName = YsbExportLogEnum.State.getName(ysbExportLog.getState());
			String createTime = DateUtil.getYYYY_MM_DD_HH_mm_ss(ysbExportLog.getCreateTime());
			String updateTime = ysbExportLog.getState() == YsbExportLogEnum.State.SUCCESS.value ? DateUtil.getYYYY_MM_DD_HH_mm_ss(ysbExportLog.getUpdateTime()) : "-";
			String url = ysbExportLog.getUrl();
			rows.add(new ToExportDto.RowDto(id, stateName, createTime, updateTime, url,ysbExportLog.getBid()));
		}

		ToExportDto dto = new ToExportDto(rows);
		model.addAttribute("dto", dto);
		return "business/tpYsbExportLog/toExportBid";
	}

	@Data
	@AllArgsConstructor
	private static class ToExportDto{
		private List<RowDto> rows;
		
		@Data
		@AllArgsConstructor
		private static class RowDto{
			private Long id; // 
			private String stateName; // 
			private String createTime;
			private String updateTime;
			private String url; // 
			private String bid; //
		}
	}
}
